const db = require("../../config/db");
// const cookieSession = require("cookie-parser");

module.exports = {
    list() {
        let sql = "select * from news_classification";
        return new Promise((resolve, reject) => {
            db.query(sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    resolve({ code: 200, rows });
                }
            });
        });
    },
    newsDetail() {
        let sql = "select * from news_detail";
        return new Promise((resolve, reject) => {
            db.query(sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    resolve({ code: 200, rows });
                }
            });
        });
    },
    // 点击指定详情
    newsClick(x) {
        let sql = `select * from news_detail where id=${x}`;
        return new Promise((resolve, reject) => {
            db.query(sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    if (rows[0].clicks == null) {
                        rows[0].clicks = 0;
                        rows[0].clicks++;
                    } else {
                        rows[0].clicks = Number(rows[0].clicks) + 1;
                    }
                    let sql_update = `UPDATE news_detail SET clicks=${rows[0].clicks} WHERE id=${x};`;
                    db.query(sql_update, (error, rows2) => {
                        if (error) {
                            reject({ code: -1, error });
                        } else {
                            resolve({ code: 200, rows });
                        }
                    });
                    // UPDATE news_detail SET clicks=1 WHERE id=1;
                    // resolve({ code: 200, rows });
                }
            });
        });
    },
    selectComment(x, y) {
        let sql = `select * from ${y} where comment_id=${x} and parent_id is null`;
        return new Promise((resolve, reject) => {
            db.query(sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    if (rows.length == 0) {
                        resolve({ code: 201, msg: "查不到该新闻评论数据" });
                    }
                    resolve({ code: 200, rows });
                }
            });
        });
    },
    searchUserNickName(x) {
        let sql = `select * from news_user where user_id=${x}`;
        return new Promise((resolve, reject) => {
            db.query(sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    if (rows.length == 0) {
                        resolve({ code: 201, msg: "该用户已注销" });
                    }
                    resolve({ code: 200, rows });
                }
            });
        });
    },
    // INSERT news_comment(comment_id,content,create_time,user_id) VALUES(2,"你说青涩最搭初恋,在风暖月光的地点","2023-03-25 10:10:10",14)
    addComment(comment_id, content, create_time, user_id, x) {
        let sql = `INSERT ${x}(comment_id,content,create_time,user_id) VALUES(${comment_id},"${content}","${create_time}",${user_id})`;
        console.log("sql,", sql);
        return new Promise((resolve, reject) => {
            db.query(sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    resolve({ code: 200, msg: "添加评论成功" });
                }
            });
        });
    },

    newsSearch(x) {
        let sql = `SELECT * from news_detail where title like '%${x}%' or digest like '%${x}%'`;
        return new Promise((resolve, reject) => {
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({ code: -1, err });
                } else {
                    resolve({ code: 200, rows });
                }
            });
        });
        // SELECT * from students where name like '%小%';
    },
    newsImgList() {
        let sql = "select * from news_images";
        return new Promise((resolve, reject) => {
            db.query(sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    resolve({ code: 200, rows });
                }
            });
        });
    },
    // 图片新闻详情
    newsImgCom(x) {
        let sql = `select * from news_images where id=${x}`;
        return new Promise((resolve, reject) => {
            db.query(sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    if (rows[0].clicks == null) {
                        rows[0].clicks = 1;
                    } else {
                        rows[0].clicks = Number(rows[0].clicks) + 1;
                    }
                    console.log(rows[0].clicks, 677);
                    let updata_sql = `UPDATE news_images SET clicks=${rows[0].clicks} WHERE id=${x};`;
                    db.query(updata_sql, (err2, rows2) => {
                        if (err2) {
                            reject({
                                code: -1,
                                err2,
                                msg: "更新数据库失败",
                            });
                        } else {
                            resolve({ code: 200, rows });
                        }
                    });
                }
            });
        });
    },
    // 新闻分类
    hotComment(x) {
        let sql = `select * from news_detail where class_id=${x} ORDER BY clicks desc`;
        return new Promise((resolve, reject) => {
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err,
                    });
                } else {
                    resolve({
                        code: 200,
                        rows,
                    });
                }
            });
        });
    },
    addLike(x, y, z, o) {
        let sql = `select * from news_like where user_db="${y}" and user_id="${x}" and comment_id=${z} and detail_id=${o}`;
        return new Promise((resolve, reject) => {
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err,
                    });
                } else {
                    console.log(rows.length);
                    if (rows.length == 0) {
                        let insert_sql = `insert into news_like(user_id,user_db,comment_id,detail_id) values(${x},"${y}",${z},${o})`;
                        db.query(insert_sql, (err2, rows2) => {
                            if (err2) {
                                reject({
                                    code: -1,
                                    err2,
                                });
                            } else {
                                resolve({
                                    code: 200,
                                    flat: 1,
                                    msg: "插入数据成功",
                                });
                            }
                        });
                    } else {
                        let del_sql = `DELETE FROM news_like where user_id=${x} and user_db="${y}" and comment_id=${z} and detail_id=${o}`;
                        db.query(del_sql, (err2, rows2) => {
                            if (err2) {
                                reject({
                                    code: -1,

                                    err2,
                                });
                            } else {
                                resolve({
                                    code: 200,
                                    flat: 0,

                                    msg: "删除数据成功",
                                });
                            }
                        });
                    }
                }
            });
        });
    },
    changeLike(x, y) {
        let select_sql = `select count(*) from news_like where detail_id=${y} and user_db="${x}"`;
        return new Promise((resolve, reject) => {
            db.query(select_sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err,
                    });
                } else {
                    resolve({
                        code: 200,
                        rows,
                    });
                }
            });
        });
    },
    selectLike(x, y, z) {
        let select_sql = `select * from news_like where detail_id=${y} and user_db="${x}" and user_id=${z}`;
        return new Promise((resolve, reject) => {
            db.query(select_sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err,
                    });
                } else {
                    console.log(rows);
                    if (rows.length == 0) {
                        resolve({
                            code: 200,
                            rows,
                            flat: 0,
                        });
                    } else {
                        resolve({
                            code: 200,
                            rows,
                            flat: 1,
                        });
                    }
                }
            });
        });
    },
    // SELECT * FROM news_detail ORDER BY update_time
    newNewsList() {
        let sql = "SELECT * FROM news_detail ORDER BY create_time desc";
        return new Promise((resolve, reject) => {
            db.query(sql, (err, row) => {
                if (err) {
                    reject({
                        code: -1,
                        err,
                    });
                } else {
                    resolve({
                        code: 200,
                        row,
                    });
                }
            });
        });
    },
    userCom(user_id) {
        let sql = `SELECT * from news_comment WHERE user_id=${user_id}`;
        return new Promise((resolve, reject) => {
            db.query(sql, (err, row) => {
                if (err) {
                    reject({
                        code: -1,
                        err,
                        msg: "数据库查询失败",
                    });
                } else {
                    resolve({
                        code: 200,
                        row,
                    });
                }
            });
        });
    },
    userLike(user_id) {
        let sql = `SELECT * from news_like WHERE user_id=${user_id}`;
        return new Promise((resolve, reject) => {
            db.query(sql, (err, row) => {
                if (err) {
                    reject({
                        code: -1,
                        err,
                        msg: "数据库查询失败",
                    });
                } else {
                    resolve({
                        code: 200,
                        row,
                    });
                }
            });
        });
    },
    userWeb(user_id) {
        let sql = `SELECT * from user_web WHERE user_id=${user_id}`;
        return new Promise((resolve, reject) => {
            db.query(sql, (err, row) => {
                if (err) {
                    reject({
                        code: -1,
                        err,
                        msg: "数据库查询失败",
                    });
                } else {
                    resolve({
                        code: 200,
                        row,
                    });
                }
            });
        });
    },
    undateUserWeb(user_id, comment_id, comment_db, comment) {
        let sql = `select * from user_web where comment_db="${comment_db}" and user_id="${user_id}" and comment_id=${comment_id}`;
        return new Promise((resolve, reject) => {
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "查询出错",
                        err,
                    });
                } else {
                    if (rows.length == 0) {
                        let insert_sql = `insert into user_web(user_id,comment_db,comment_id,comment) values(${user_id},"${comment_db}",${comment_id},"${comment}")`;
                        db.query(insert_sql, (err2, rows2) => {
                            if (err2) {
                                reject({
                                    code: -1,
                                    msg: "插入失败",
                                    err2,
                                });
                            } else {
                                resolve({
                                    code: 200,
                                    msg: "插入成功",
                                    rows2,
                                });
                            }
                        });
                    } else {
                        resolve({
                            code: 200,
                            msg: "查询成功",
                            rows,
                        });
                    }
                }
            });
        });
    },
    searchNewsLike(user_db, detail_id) {
        let sql = `select * from ${user_db} where id=${detail_id}`;
        return new Promise((resolve, reject) => {
            db.query(sql, (err, row) => {
                if (err) {
                    reject({
                        code: -1,
                        err,
                        msg: "查询失败",
                    });
                } else {
                    resolve({
                        code: 200,
                        row,
                        msg: "查询成功",
                    });
                }
            });
        });
    },
    insertCollect(user_id, comment_id, comment_db) {
        let sql = `select * from news_collect where comment_db="${comment_db}" and user_id="${user_id}" and comment_id=${comment_id}`;
        return new Promise((resolve, reject) => {
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err,
                        msg: "查询数据库失败",
                    });
                } else {
                    if (rows.length == 0) {
                        let insert_sql = `insert into news_collect(user_id,comment_db,comment_id) values(${user_id},"${comment_db}",${comment_id})`;
                        db.query(insert_sql, (err2, rows2) => {
                            if (err2) {
                                reject({
                                    code: -1,
                                    err2,
                                    msg: "插入失败",
                                });
                            } else {
                                rows2["flat"] = 1;
                                resolve({
                                    code: 200,
                                    rows2,
                                    msg: "插入成功",
                                });
                            }
                        });
                    } else {
                        let del_sql = `DELETE FROM news_collect where user_id=${user_id} and comment_db="${comment_db}" and comment_id=${comment_id}`;
                        db.query(del_sql, (err2, rows2) => {
                            if (err2) {
                                reject({
                                    code: -1,
                                    err2,
                                    msg: "删除失败",
                                });
                            } else {
                                rows2["flat"] = 0;

                                resolve({
                                    code: 200,
                                    rows2,
                                    msg: "删除成功",
                                });
                            }
                        });
                    }
                }
            });
        });
    },
    selectCollect(user_id, comment_id, comment_db) {
        let sql = `select * from news_collect where comment_db="${comment_db}" and user_id="${user_id}" and comment_id=${comment_id}`;
        return new Promise((resolve, reject) => {
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err,
                        msg: "查询数据库失败",
                    });
                } else {
                    if (rows.length == 0) {
                        resolve({
                            code: 200,
                            rows,
                            flat: 0,
                            msg: "查询数据库成功",
                        });
                    } else {
                        resolve({
                            code: 200,
                            rows,
                            flat: 1,

                            msg: "查询数据库成功",
                        });
                    }
                }
            });
        });
    },
    selectCollectList(user_id, comment_db) {
        let sql = `select * from news_detail,news_collect where news_detail.id = news_collect.comment_id and news_collect.user_id=${user_id} and news_collect.comment_db="${comment_db}"`;
        return new Promise((resolve, reject) => {
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err,
                        msg: "查询失败",
                    });
                } else {
                    resolve({
                        code: 200,
                        rows,
                        msg: "查询成功",
                    });
                }
            });
        });
    },
    // insert into news_detail(title,source,digest,content,class_id) values("赌神归来","萧炎","无敌是多么寂寞","躺在木盆内，身子侵在冰凉的灵液之中，萧炎有些沉醉于体内的那股充实的力量之感，按照寻常的突破模式，一般在到达第七段之后会有一段时间的温养期，而有着筑基灵液相助的萧炎，却是直接跳过了这时期，到达了充盈阶段。",1)
    addNews(title, source, digest, create_time, content, class_id, hot) {
        let insert_sql = `insert into news_detail(title,source,digest,create_time,content,class_id,hot) values("${title}","${source}","${digest}","${create_time}","${content}",${class_id},${hot})`;
        return new Promise((resolve, reject) => {
            db.query(insert_sql, (err, row) => {
                if (err) {
                    reject({
                        code: -1,
                        err,
                        msg: "发布失败",
                    });
                } else {
                    resolve({
                        code: 200,
                        row,
                        msg: "发布成功",
                    });
                }
            });
        });
    },
    newsDetailPaging(page_num = 1, page_size = 10) {
        return new Promise((resolve, reject) => {
            let startIndex = (page_num - 1) * page_size;
            let size = page_size;
            // 分页数据
            let sql = "select * from news_detail limit " + startIndex + "," + size + "";
            db.query(sql, function (err, rows) {
                if (err) {
                    resolve({
                        code: -1,
                        msg: "查询轮播图失败。",
                        err,
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "获取分页列表成功。",
                        result: rows,
                        page: {
                            page_num: page_num,
                            page_size: page_size,
                        },
                    });
                }
            });
        });
    },
    newsDetailTotal() {
        let sql = "select count(*) from news_detail";
        return new Promise((resolve, reject) => {
            db.query(sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    resolve({ code: 200, rows });
                }
            });
        });
    },
    newsSearchPaging2(content, page_num = 1, page_size = 10) {
        console.log(123);
        return new Promise((resolve, reject) => {
            let startIndex = (page_num - 1) * page_size;
            let size = page_size;
            // 分页数据
            console.log(startIndex, 5656);
            let sql = `select * from news_detail where title like '%${content}%' or digest like '%${content}%' limit ${startIndex}, ${size} `;
            db.query(sql, function (err, rows) {
                if (err) {
                    reject({
                        code: -1,
                        msg: "查询分页列表失败。",
                        err,
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "获取分页列表成功。",
                        result: rows,
                        page: {
                            page_num: page_num,
                            page_size: page_size,
                        },
                    });
                }
            });
        });
    },
    delNews(id) {
        let del_sql = `DELETE FROM news_detail where id=${id}`;
        return new Promise((resolve, reject) => {
            db.query(del_sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        err,
                        msg: "删除失败",
                    });
                } else {
                    resolve({
                        code: 200,
                        rows,
                        msg: "删除成功",
                    });
                }
            });
        });
    },
    addSonComment(parent_id, comment_id, content, create_time, user_id, db_id) {
        let sql = `INSERT ${db_id}(comment_id,content,create_time,user_id,parent_id) VALUES(${comment_id},"${content}","${create_time}",${user_id},${parent_id})`;
        console.log("sql,", sql);
        return new Promise((resolve, reject) => {
            db.query(sql, (err, rows) => {
                if (err) {
                    reject(err);
                } else {
                    resolve({ code: 200, msg: "添加子评论成功" });
                }
            });
        });
    },
    // select * from news_detail,news_collect where news_detail.id = news_collect.comment_id and news_collect.user_id=22 and news_collect.comment_db="comment"

    selectSonComment(parent_id) {
        let sql = `select * from news_comment,news_user where news_comment.user_id=news_user.user_id and news_comment.parent_id=${parent_id}`;
        return new Promise((resolve, reject) => {
            db.query(sql, (err, rows) => {
                if (err) {
                    reject({
                        code: -1,
                        msg: "查询子评论失败",
                        err,
                    });
                } else {
                    resolve({
                        code: 200,
                        msg: "查询子评论成功",
                        rows,
                    });
                }
            });
        });
    },
};
// select * from news_detail,news_collect where news_detail.id = news_collect.comment_id and news_collect.user_id=22
